package com.easylive.web.controller;

import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.dto.UserMessageCountDTO;
import com.easylive.entity.dto.UserMessageExtendDTO;
import com.easylive.entity.enums.MessageReadTypeEnum;
import com.easylive.entity.enums.MessageTypeEnum;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.po.UserMessage;
import com.easylive.entity.query.UserMessageQuery;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserMessageService;
import com.easylive.utils.JsonUtil;
import com.easylive.web.annotation.GlobalInterceptor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/24 22:33
 **/
@RequestMapping("/message")
@RestController
public class UserMessageController extends ABaseController {

    @Resource
    private UserMessageService userMessageService;

    @PostMapping("/getNoReadCount")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getNoReadCount() {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        // 获取未读信息
        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setUserId(tokenUserInfoDto.getUserId());
        userMessageQuery.setReadType(MessageReadTypeEnum.UNREAD.getType());

        Integer count = userMessageService.findCountByParam(userMessageQuery);

        return getSuccessResponseVO(count);
    }

    @PostMapping("/getNoReadCountGroup")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getNoReadCountGroup() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        List<UserMessageCountDTO> list = userMessageService.findNoReadCountGroup(tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(list);
    }

    @PostMapping("/readAll")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO readAll(@Validated @NotNull Integer messageType) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userMessageService.readAllMessages(tokenUserInfoDto.getUserId(), messageType);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadMessage")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadMessage(@Validated Integer pageNo, @NotNull Integer messageType) {
        MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByType(messageType);
        if (messageTypeEnum == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserMessageQuery userMessageQuery = new UserMessageQuery();
        userMessageQuery.setUserId(tokenUserInfoDto.getUserId());
        userMessageQuery.setMessageType(messageType);
        userMessageQuery.setOrderBy("message_id desc");
        userMessageQuery.setQueryUserInfo(true);
        userMessageQuery.setQueryVideoInfo(true);
        userMessageQuery.setPageNo(pageNo == null ? Constants.ONE : pageNo);

        PaginationResultVO<UserMessage> page = userMessageService.findListByPage(userMessageQuery);
        List<UserMessage> userMessageList = page.getList();
        if (userMessageList.size() > 0) {
            userMessageList.forEach(userMessage -> {
                userMessage.setExtendDto(JsonUtil.convertStrToObj(userMessage.getExtendJson(), UserMessageExtendDTO.class));
            });
        }

        return getSuccessResponseVO(page);
    }

    @PostMapping("/delMessage")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delMessage(@Validated @NotNull Integer messageId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userMessageService.deleteMessage(tokenUserInfoDto.getUserId(), messageId);
        return getSuccessResponseVO(null);
    }
}
